<template>
	<div>
		<div class="flex flex_b">
			<div>
				<el-button size="mini" type="primary" style="margin-top: 10px;position: relative;"
				class="el-dialog-position">
				    <span>
				        导入<i class="el-icon-upload el-icon--right" />
				        <input style="opacity: 0;border: 1px solid #000;position: absolute;
							width: 70px;top: 0;left: 0;" 
							ref="files" type="file" class="excelFile" @change="excelFileMethod" />
				    </span>
				</el-button>
				<el-button size="small" @click="saveBuyer">
				    确定导入
				</el-button>
				<span v-if="ttype==1" style="color: red;margin-left: 10px;">*红色商品名称为已经入库商品</span>
				<span v-if="ttype==2" style="color: red;margin-left: 10px;">*红色串号为已经出库串号</span>
			</div>
			<div>
				<div style="display: flex;margin-top: 10px;">
				  <div style="margin-right: 10px;">
					  总计单据：
				       <el-input size="mini" v-model="sumOrder" 
					   disabled
				      style="width:150px;"></el-input>  
				  </div>
				  <div>
					  总计数量：
				      <el-input size="mini" v-model="list.length" 
					  disabled
				      style="width:150px;"></el-input>
				  </div>
				</div>
			</div>
		</div>
		
		<br> <br>
		<el-table  :data="list"
			 border  
			>
			<!-- <el-table-column
			      type="selection"
			      width="55">
			    </el-table-column> -->
			<el-table-column  label="操作" width="60"  align="center">
			    <template slot-scope="scope">
			        <i class="el-icon-delete" style="cursor: pointer;" 
					@click.stop="delRow(scope)"></i>
			    </template>
			</el-table-column>	
			<el-table-column   align="center"  width="60"
				label="序号">
				<template slot-scope="scope">
				   <div>
						{{scope.$index+1}}
				   </div>
				</template>
			</el-table-column>	
			<el-table-column   align="center"  width="200px"
				label="速记码" prop="mcode" v-if="ttype==1" />
			<el-table-column   align="center"
							width="150px"
							v-if="ttype==1"
							label="物流单号" prop="logistics_number" />
			<el-table-column label="供应商" align="center" width="150px">
				<template slot-scope="scope">
				   <div v-if="scope.row.is_not_supplier==1" style="color: red;">
						{{scope.row.supplier}}
				   </div>
				   <div v-else>
					   {{scope.row.supplier}}
				   </div>
				</template>
			</el-table-column>
			
			<el-table-column label="商品名称" align="center" width="260px">
				<template slot-scope="scope">
				   <div v-if="scope.row.is_inster==1" style="color: red;">
						{{scope.row.goods_name}}
				   </div>
				   <div v-else>
					   {{scope.row.goods_name}}
				   </div>
				</template>
			</el-table-column>	
		    
			<el-table-column label="商品数量" align="center" width="80px">
				<template slot-scope="scope">
				   <div v-if="scope.row.type==1 || scope.row.type==2">
						{{scope.row.goods_number}}
				   </div>
				   <div v-else>
					   -{{scope.row.goods_number}}
				   </div>
				</template>
			</el-table-column>
			<el-table-column label="单价" align="center" width="100px">
				<template slot-scope="scope">
				   <div v-if="scope.row.type==1 || scope.row.type==2">
				   		{{scope.row.price}}
				   </div>
				   <div v-else>
				   	   -{{scope.row.price}}
				   </div>
				</template>
			</el-table-column>
			
			<el-table-column label="串号" align="center" width="150px">
				<template slot-scope="scope">
				   <div v-if="scope.row.is_wholesaler==1">
				   		{{scope.row.code}}
				   </div>
				   <div v-else style="color: red;">
				   	   {{scope.row.code}}
				   </div>
				</template>
			</el-table-column>
			
			<el-table-column   align="center"
							label="备注" prop="remark" />
			<el-table-column   align="center" width="150px"
							label="制单日期" prop="add_time" />
				
			<el-table-column label="单据号" align="center" width="80px">
				<template slot-scope="scope">
				   <div>
						{{$utils.sliceStr(scope.row.order_number,6)}}
				   </div>
				</template>
			</el-table-column>
			<el-table-column label="仓库" align="center" width="100px">
				<template slot-scope="scope">
				   <div v-if="scope.row.isjc==0">
				   	   总仓
				   </div>
				   <div v-else>
				   	   寄存
				   </div>
				</template>
			</el-table-column>
			
		</el-table>
	</div>
</template>

<script>
	import * as XLSX from 'xlsx'
	export default {
		props:{
			ttype:{
				type:String,
				default:'1'  // 1 采购 采购退货  2 批发 批发退货
			}
		},
		data(){
			return {
				list:[]
			}
		},
		computed:{
			sumOrder() {
				let arr = []
				for(let item of this.list){
					let index = arr.findIndex((el)=>{
						return el == item.order_number
					})
					if(index==-1){
						arr.push(item.order_number)
					}
				}
				return arr.length
			},
		},
		methods:{
			saveBuyer(){
				for(let item of this.list){
					item.buyer_person = sessionStorage.getItem('user_name')
					if(item.type==1 || item.type==2){
						item.codeArr = [item.code]
						item.code_arr = JSON.stringify([item.code])
						if(item.type == 1){
							item.initial_price = item.price
						}
						if(item.is_not_supplier==1) return this.$message.error('该用户不是供应商')
					}else{
						if(item.is_not_supplier==1) return this.$message.error('该用户不是客户')
					}
					
					item.code_arr_length = 1
					item.money = item.price
					item.mcode = undefined
					if(item.type!=1 && item.type!=0){
						item.isjc = '0'
					}
					if(item.type==3){
						item.price = 0
						item.money = 0
					}
					if(!item.goods_name) return this.$message.error('商品名称不能为空') 
					
				}
				
				this.list = this.list.filter((item)=>{
					return item.is_not_supplier != 1
				})
				
				let arr1 = this.list.filter((item)=>{
					return item.isjc == 1 && item.is_inster != 1
				})
				
				let arr2 = this.list.filter((item)=>{
					return item.isjc == 0 && item.is_inster != 1
				})
				
				if(arr1.length){
					for(let item of arr1){
						item.isjc = undefined
					}
					this.$post('/buyerSysJc/addEditBuyerJc',arr1,res1=>{
						let result = res1.data
						if(result.code==200){
							this.list = []
						}
					})
				}else{
					this.list = []
				}
				
				if(arr2.length){
					for(let item of arr2){
						item.isjc = undefined
					}
					this.$post('/buyerSys/addEditBuyer',arr2,res2=>{
						let result = res2.data
						if(result.code==200){
							this.list = []
							this.$message.success('操作成功')
						}
					})
				}else{
					this.list = []
				}
				
			},
			delRow(row){
				let index = row.$index
				this.list.splice(index,1)
			},
			initData(excelData){
				
				
				let arr = []
				for(let item of excelData){
					 let type = ''
					 if(this.ttype==1){
						 if(item['入库方式']=='购') type=1
						 if(item['入库方式']=='购退') type=0
					 }else{
						 if(item['数量']=='1') type=2
						 if(item['数量']=='-1') type=3
					 }
					 
					 
					 let obj = {
						 code:item['串号'],
						 buyer_date:this.ttype==1?item['入库日期']:item['出库日期'],
						 supplier:this.ttype==1?item['供应商']:item['客户'],
						 price:item['单价'],
						 type:type,
						 remark:item['明细备注'] || '',
						 add_time:this.ttype==1?item['入库时间']:item['制单时间'],
						 logistics_number:this.ttype==1?item['物流单号']:'',
						 cid:sessionStorage.getItem('cid'),
						 order_number:'1111'+ item['单据号']+'22222',
						 isjc:item['仓库']=='总仓'?'0':'1'
					 }
					 if(this.ttype==1){
						 obj.mcode = item['助记码']
					 }else{
						 obj.goods_name = item['商品']
					 }
					
					 arr.push(obj)
				}
				
				let params = {
					excelData:arr
				}
				this.$post('/buyerSys/exportBuyerData',params,res=>{
					console.log(res)
					let result = res.data
					if(result.code == 200){
						for(let item of result.data){
							item.goods_number = 1
						}
						this.list = result.data
						this.$refs.files.value = ''
					}
				})
			},
			excelFileMethod(e) {
				console.log(e)
			    var _this = this
				//  excel文件信息
				const files = e.target.files
				console.log(files,3333);
				// 构建fileReader对象
				const fileReader = new FileReader()
				// 读取操作完成时
				fileReader.onload = function(ev) {
				
					try {
						 const data = ev.target.result
						 const wb = XLSX.read(data, {type: 'binary'})
						
						 const excelData =  XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
						 _this.initData(excelData)
						 
						
					} catch (e) {
						return
					}
				}
				// 读取指定文件内容
			   fileReader.readAsBinaryString(files[0])
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	
</style>